package com.inifitness.admin.core.system.controller;

import java.util.List;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.inifitness.admin.core.config.resubmit.annotation.PreventDuplicateSubmit;
import com.inifitness.admin.core.system.model.form.DictForm;
import com.inifitness.admin.core.system.model.form.DictItemForm;
import com.inifitness.admin.core.system.model.query.DictItemPageQuery;
import com.inifitness.admin.core.system.model.query.DictPageQuery;
import com.inifitness.admin.core.system.model.vo.DictItemPageVO;
import com.inifitness.admin.core.system.model.vo.DictPageVO;
import com.inifitness.admin.core.system.service.SysDictItemService;
import com.inifitness.admin.core.system.service.SysDictService;
import com.inifitness.common.model.Option;
import com.inifitness.common.result.PageResult;
import com.inifitness.common.result.Result;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "06.字典接口")
@RestController
@RequestMapping("/api/v1/dict")
@RequiredArgsConstructor
public class SysDictController {

  private final SysDictItemService dictService;

  private final SysDictService dictTypeService;

  @Operation(summary = "字典明细分页列表")
  @GetMapping("/item/page")
  public PageResult<DictItemPageVO> getDictItemPage(
      @ParameterObject DictItemPageQuery queryParams) {
    Page<DictItemPageVO> result = dictService.getDictItemPage(queryParams);
    return PageResult.success(result);
  }

  @Operation(summary = "字典明细数据表单数据")
  @GetMapping("/item/{id}/form")
  public Result<DictItemForm> getDictItemForm(
      @Parameter(description = "字典ID") @PathVariable(value = "id") Long id) {
    DictItemForm formData = dictService.getDictItemForm(id);
    return Result.success(formData);
  }

  @Operation(summary = "新增字典明细")
  @PostMapping("/item")
  @PreAuthorize("@ss.hasPerm('sys:dict:add')")
  @PreventDuplicateSubmit
  public Result saveDictItem(
      @RequestBody DictItemForm form
  ) {
    boolean result = dictService.saveDictItem(form);
    return Result.judge(result);
  }

  @Operation(summary = "修改字典明细")
  @PutMapping("/item/{id}")
  @PreAuthorize("@ss.hasPerm('sys:dict:edit')")
  public Result updateDictItem(
      @PathVariable(value = "id") Long id,
      @RequestBody DictItemForm form
  ) {
    boolean status = dictService.updateDictItem(id, form);
    return Result.judge(status);
  }

  @Operation(summary = "删除字典明细")
  @DeleteMapping("/item/{ids}")
  @PreAuthorize("@ss.hasPerm('sys:dict:delete')")
  public Result deleteDictItem(
      @Parameter(description = "字典ID，多个以英文逗号(,)拼接") @PathVariable(value = "ids") String ids
  ) {
    boolean result = dictService.deleteDictItem(ids);
    return Result.judge(result);
  }


  @Operation(summary = "字典明细下拉列表")
  @GetMapping("/item/{dictCode}/options")
  public Result<List<Option>> listDictItemOptions(
      @Parameter(description = "字典类型编码") @PathVariable(value = "dictCode") String dictCode) {
    List<Option> list = dictService.listDictItemOptions(dictCode);
    return Result.success(list);
  }


  /*----------------------------------------------------*/
  @Operation(summary = "字典分页列表")
  @GetMapping("/page")
  public PageResult<DictPageVO> getDictPage(
      @ParameterObject DictPageQuery queryParams
  ) {
    Page<DictPageVO> result = dictTypeService.getDictPage(queryParams);
    return PageResult.success(result);
  }

  @Operation(summary = "字典表单数据")
  @GetMapping("/{id}/form")
  public Result<DictForm> getDictForm(
      @Parameter(description = "字典ID") @PathVariable(value = "id") Long id
  ) {
    DictForm dictForm = dictTypeService.getDictForm(id);
    return Result.success(dictForm);
  }

  @Operation(summary = "新增字典")
  @PostMapping()
  @PreAuthorize("@ss.hasPerm('sys:dict:add')")
  @PreventDuplicateSubmit
  public Result saveDict(@RequestBody DictForm dictForm) {
    boolean result = dictTypeService.saveDict(dictForm);
    return Result.judge(result);
  }

  @Operation(summary = "修改字典")
  @PutMapping("/{id}")
  @PreAuthorize("@ss.hasPerm('sys:dict:edit')")
  public Result updateDict(@PathVariable(value = "id") Long id, @RequestBody DictForm dictForm) {
    boolean status = dictTypeService.updateDict(id, dictForm);
    return Result.judge(status);
  }

  @Operation(summary = "删除字典")
  @DeleteMapping("/{ids}")
  @PreAuthorize("@ss.hasPerm('sys:dict:delete')")
  public Result deleteDicts(
      @Parameter(description = "字典类型ID，多个以英文逗号(,)分割") @PathVariable(value = "ids") String ids
  ) {
    boolean result = dictTypeService.deleteDicts(ids);

    return Result.judge(result);
  }
}
